package com.ukee.birthday.modules.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ukee.birthday.common.utils.PageUtils;
import com.ukee.birthday.common.utils.Query;
import com.ukee.birthday.modules.dao.StaffDao;
import com.ukee.birthday.modules.entity.StaffEntity;
import com.ukee.birthday.modules.entity.SysUserEntity;
import com.ukee.birthday.modules.service.StaffService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service("staffService")
public class StaffServiceImpl extends ServiceImpl<StaffDao, StaffEntity> implements StaffService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String username = (String)params.get("username");
        String sex = (String)params.get("sex");
        String role = (String)params.get("role");
        String startTime = (String)params.get("startTime");
        String endTime = (String)params.get("endTime");

        IPage<StaffEntity> page = this.page(
                new Query<StaffEntity>().getPage(params),
                new QueryWrapper<StaffEntity>()
                        .like(StringUtils.isNotBlank(username),"username", username)
                        .ne("username","admin")
                        .eq(StringUtils.isNotBlank(sex),"sex", sex)
                        .eq(StringUtils.isNotBlank(role),"role_id", role)
                        .gt(StringUtils.isNotEmpty(startTime),"birthday",startTime)
                        .lt(StringUtils.isNotEmpty(endTime),"birthday",endTime)
                        .orderByDesc("create_time")
        );
        return new PageUtils(page);
    }

    @Override
    public StaffEntity queryByUserName(String username) {
        return baseMapper.queryByUserName(username);
    }

    @Override
    public StaffEntity queryByTel(String tel) {
        return baseMapper.queryByTel(tel);
    }


    @Override
    public boolean updatePassword(Long userId, String password, String newPassword) {
        StaffEntity staffEntity = new StaffEntity();
        staffEntity.setPassword(newPassword);
        return this.update(staffEntity,
                new QueryWrapper<StaffEntity>().eq("id", userId).eq("password", password));
    }

}
